package com.android.plc.ui.manager;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.net.Uri;
import android.text.Editable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.android.plc.R;
import com.android.plc.classholder.BasicItem.Type;
import com.android.plc.classholder.Home;
import com.android.plc.util.PrefUtil;

public class HomeRemoteMagager extends BaseManager<Home>
{

	private static final boolean DEFAULT_IS_SINGLE = true;
	private static final int DEFAULT_USER_CODE = 1;
	private Button mSingleBtn;
	private Button mThreeBtn;

	private EditText mTxtHomeName;
	private EditText mTxtUsercode;
	private EditText mTxtAddress;
	private Home mHome;

	public HomeRemoteMagager(Context context, ViewGroup view)
	{
		super(context, view, R.layout.home_setting);
	}

	@Override
	public void initView(ViewGroup view)
	{
		mSingleBtn = (Button) view.findViewById(R.id.btnSingle);
		mThreeBtn = (Button) view.findViewById(R.id.btnThree);
		mTxtHomeName = (EditText) view.findViewById(R.id.txtHomeName);
		mTxtUsercode = (EditText) view.findViewById(R.id.txtUserCode);
		mTxtAddress = (EditText) view.findViewById(R.id.txtAddr);

		mHome = new Home(Type.NORMAL_ITEM);
		SharedPreferences pref = PrefUtil.getPreferences(mContext);
		Resources res = mContext.getResources();
		mHome.name = pref.getString(PrefUtil.PREF_HOME_NAME, res.getString(R.string.default_home_name));
		mHome.user_code = pref.getInt(PrefUtil.PREF_USER_CODE, DEFAULT_USER_CODE);
		mHome.singlePhase = pref.getBoolean(PrefUtil.PREF_IS_SINGLE, DEFAULT_IS_SINGLE);
		mHome.address = pref.getString(PrefUtil.PREF_ROMOTE_ADDRESS, res.getString(R.string.default_address));
		displayItem(mHome);

		mSingleBtn.setOnClickListener(this);
		mThreeBtn.setOnClickListener(this);
		mTxtHomeName.addTextChangedListener(this);
		mTxtUsercode.addTextChangedListener(this);
		mTxtAddress.addTextChangedListener(this);
	}

	public void afterTextChanged(Editable s)
	{
		if (checkValidity(getView()))
		{
			int user_code = mHome.user_code;
			String code = mTxtUsercode.getText().toString();
			if (code.length() == 0)
			{
				return;
			}
			try
			{
				user_code = Integer.valueOf(code);
			} catch (Exception e)
			{
				return ;
			}
			
			if (user_code <= 0 || user_code > 250)
			{
				AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
				builder.setTitle(mContext.getString(android.R.string.dialog_alert_title))
						.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
						{
							public void onClick(DialogInterface dialog, int which)
							{
								dialog.dismiss();
							}
						}).setMessage(R.string.user_code_hint).create();
				builder.show();
				mTxtUsercode.setText("" + (mHome.user_code));
				return;
			}

			String newHomeName = mTxtHomeName.getText().toString().trim();
			boolean selected = mSingleBtn.isSelected();
			String newAddress = mTxtAddress.getText().toString().trim();
			boolean same = newHomeName.equals(mHome.name);
			same = same && mHome.user_code == user_code;
			same = same && selected == mHome.singlePhase;
			same = same && newAddress.equals(mHome.address);
			if (!same)
			{
				mHome.name = newHomeName;
				mHome.user_code = user_code;
				mHome.singlePhase = selected;
				mHome.address = newAddress;
				update(mHome);
			}
		}
	}

	public void onClick(View v)
	{
		mHome.singlePhase = v == mSingleBtn;
		if (mHome.singlePhase)
		{
			mSingleBtn.setSelected(true);
			mThreeBtn.setSelected(false);
		} else
		{
			mSingleBtn.setSelected(false);
			mThreeBtn.setSelected(true);
		}
		update(mHome);
	}

	@Override
	public boolean checkValidity(View view)
	{
		return true;
	}

	@Override
	public Uri getUri()
	{
		return null;
	}

	@Override
	public void displayItem(Home item)
	{
		if (item.singlePhase)
		{
			mSingleBtn.setSelected(true);
			mThreeBtn.setSelected(false);
		} else
		{
			mSingleBtn.setSelected(false);
			mThreeBtn.setSelected(true);
		}
		mTxtHomeName.setText(item.name);
		mTxtUsercode.setText("" + (item.user_code));
		mTxtAddress.setText(item.address);

	}

	@Override
	public ContentValues item2maps(Home newT)
	{
		return null;
	}

	@Override
	public boolean insert(Home newT)
	{
		return update(newT);
	}

	@Override
	public boolean update(Home newT)
	{
		SharedPreferences pref = PrefUtil.getPreferences(mContext);
		Editor editor = pref.edit();
		editor.putString(PrefUtil.PREF_HOME_NAME, newT.name);
		editor.putInt(PrefUtil.PREF_USER_CODE, newT.user_code);
		editor.putBoolean(PrefUtil.PREF_IS_SINGLE, newT.singlePhase);
		editor.putString(PrefUtil.PREF_ROMOTE_ADDRESS, newT.address);
		editor.commit();
		return true;
	}

}
